/*
 * ComerciosBusquedaFrame.java
 *
 * Created on 16 de abril de 2008, 11:45 PM
 */
package org.comercios.registros;

import org.mybeans.Sesion;
import org.mybeans.ComerciosBeans;
 
import org.mybeans.ComerciosInscripcionBeans;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  avbravo
 */
public class ComerciosInscritosFrame extends javax.swing.JFrame {

    Sesion sesion;
    int fila = -1;
    DefaultTableModel modelo;
    ComerciosBeans comerciosBeans;
    ComerciosInscripcionBeans comerciosInscripcionBeans;

    /** Creates new form ComerciosBusquedaFrame */
    public ComerciosInscritosFrame() {
        initComponents();
        sesion = new Sesion();
        comerciosBeans = new ComerciosBeans();
        /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */

        //String sql = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' and comercios.activo ='si' and comercios.cedula = contribuyentes.cedula order by comercio";
        String sql = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes, comerciosinscripcion where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' and comercios.activo ='si' and comercios.cedula = contribuyentes.cedula" +
                " and comerciosinscripcion.idcomercio = comercios.idcomercio" +
                " and comerciosinscripcion.activo='si'" +
                " and comerciosinscripcion.idmunicipio = comercios.idmunicipio" +
                " and contribuyentes.idmunicipio = comercios.idmunicipio" +
                " order by comercio";
        CargarTodosRegistros(sql);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonRegresar = new javax.swing.JButton();
        jButtonVerNoInscritos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();
        jButtonVerInscritos = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("COMERCIOS INSCRITOS");

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
         

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonVerNoInscritos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/noinscritos.jpeg"))); // NOI18N
        jButtonVerNoInscritos.setToolTipText("Ver No inscritos");
        jButtonVerNoInscritos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerNoInscritosActionPerformed(evt);
            }
        });

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "codigo", "comercio", "contribuyente", "cedula"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jXTable1);

        jButtonVerInscritos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/inscritos.png"))); // NOI18N
        jButtonVerInscritos.setToolTipText("Ver Inscritos");
        jButtonVerInscritos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerInscritosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jButtonVerInscritos, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonVerNoInscritos, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 680, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonVerInscritos))
                    .addComponent(jButtonVerNoInscritos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonVerNoInscritosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerNoInscritosActionPerformed

    String sql = "select comercios.*, contribuyentes.contribuyente " +
            " from comercios, contribuyentes, comerciosinscripcion " +
            " where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' "+
            " and comercios.activo ='si' and comercios.cedula = contribuyentes.cedula " +
            " and contribuyentes.idmunicipio ='" + sesion.getIdmunicipio() + "' "+
  "          and comercios.idcomercio not in "+
 " (select distinct comerciosinscripcion.idcomercio from comerciosinscripcion "+
  " where comerciosinscripcion.idmunicipio ='" + sesion.getIdmunicipio() + "' "+
   " and comerciosinscripcion.activo='si' ) "+
  " order by comercio ";
            

    CargarTodosRegistros(sql);
}//GEN-LAST:event_jButtonVerNoInscritosActionPerformed

private void jButtonVerInscritosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerInscritosActionPerformed
    // TODO add your handling code here:
       String sql = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes, comerciosinscripcion where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' and comercios.activo ='si' and comercios.cedula = contribuyentes.cedula" +
                " and comerciosinscripcion.idcomercio = comercios.idcomercio" +
                " and comerciosinscripcion.activo='si'" +
                " and comerciosinscripcion.idmunicipio = comercios.idmunicipio" +
                " and contribuyentes.idmunicipio = comercios.idmunicipio" +
                " order by comercio";


    CargarTodosRegistros(sql);
}//GEN-LAST:event_jButtonVerInscritosActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosInscritosFrame().setVisible(true);
            }
        });
    }

   
    public void CargarTodosRegistros(String sql) {
        try {

            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            ResultSet rs = psquery.executeQuery();

            modelo = new DefaultTableModel();
            // Creamos las columnas.
            modelo.addColumn("codigo");
            modelo.addColumn("comercio");
            modelo.addColumn("contribuyente");
            modelo.addColumn("cedula");

// Bucle para cada resultado en la consulta
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[4]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idcomercio");
                fila[1] = rs.getString("comercio");
                fila[2] = rs.getString("contribuyente");
                fila[3] = rs.getString("cedula");
                modelo.addRow(fila);

            }
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);
            //aris++
            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                        String contribuyente = String.valueOf(jXTable1.getValueAt(fila, 2));
                        String cedula = String.valueOf(jXTable1.getValueAt(fila, 3));

                      

                    }
                }
            });

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error. CargarTodosRegistros()", JOptionPane.ERROR_MESSAGE);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVerInscritos;
    private javax.swing.JButton jButtonVerNoInscritos;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
